<?php

namespace app\backend\controller;

use app\common\model\Region;

/**
 * 登录类
 * @author Ray 541720500@qq.com
 */
class Login extends Base
{
    public function loginform()
    {
        return $this->fetch('index');
//   dump(distance(36.49611352,117.0087719, 36.70255857, 117.0648193359));
        
        // 生成省市区js
//        $province = Region::where('pid', 0)->select();
//
//        $province_list = $city_list = $county_list = [];
//        foreach ($province as $k1 => $p) {
//            $k1++;
//            $tmp1 = $k1<10 ? ('0'.$k1) : $k1;
//            $province_list[strval(str_pad($tmp1,6,"0"))] = $p['name'];
//            $city = Region::where('pid', $p['id'])->select();
//            foreach ($city as $k2 => $c) {
//                $k2++;
//                $tmp2 = $k2<10 ? ('0'.$k2) : $k2;
//                $city_list[ strval($tmp1. str_pad( $tmp2, 4, "0"))] = $c['name'];
//                $county = Region::where('pid', $c['id'])->select();
//                foreach ($county as $k3 => $o) {
//                    $k3++;
//                    $county_list[strval($tmp1. $tmp2 . ($k3<10 ? ('0'.$k3) : $k3))] = $o['name'];
//                }
//            }
//
//        }
//        $json = json_encode(compact('province_list','city_list','county_list'),JSON_UNESCAPED_UNICODE);
//        $a = file_put_contents('./region.js', $json);
//        var_dump($a);
//        die;
//        $data['city_list'] = Region::where('level',2)->select();
//        $data['county_list'] = Region::where('level',3)->select();
//        echo json_encode($data);die;

    }

    public function paymentForm()
    {
        return $this->fetch();
    }

    public function login()
    {
        if (request()->isPost() == false)
            $this->error('页面不存在');

        // 验证码验证
//        $captcha = new \think\captcha\Captcha();
//        if (!$captcha->check(input('code')))
//            $this->error('验证码错误');

        // 清除管理员id的session值
        session('adm_id', null);
        //验证账号和密码
        $un = trim(input('post.username'));
        $pw = input('post.password');
        $model_adm = model('auth');
        $adm = $model_adm->where(array('account' => $un))->find();
        if (!$adm || $adm['password'] != md500($pw)) {
            $this->error('账号不存在或密码错误');
        } elseif ($adm['status'] === 0) {
            $this->error('账号被锁定');
        } else {
            //更新最后登录时间和登陆ip
            $data = [
                'login_time' => now(),
                'ip' => request()->ip(),
            ];
            $model_adm->save($data, ['id' => $adm['id']]);
            $model_adm->where('id', $adm['id'])->setInc('login_num'); // 登录次数+1
            // 写入session
            session('adm_id', $adm['id']);
            session("account", $adm['account']);
            session('ip', $adm['ip']);
            session('last_login_time', $adm['login_time']);
            // 记录管理员日志
            $this->makeLog("[登录成功]");
            // 获取权限节点列表
            $this->redirect('index/index');
        }
    }

    public function logout()
    {
        session('adm_id', null);
        session(null);
        session(config('status.auth.user_auth_nodes'), null);
        $this->redirect('loginform');
    }
}
